//logs.js
const util = require('../../../utils/util.js')
const app = getApp();
const handler = require("../../../utils/function.js");
const data = require("../../../data/mockData.js");
var api = require("../../../api.js");
const openId = wx.getStorageSync("openId");
Page({
  data: {
    logs: []
  },
  onLoad: function () {
   
    // this.setData({
    //   logs: (wx.getStorageSync('logs') || []).map(log => {
    //     return util.formatTime(new Date(log))
    //   })
    // })
  },
  fan(){
    wx.navigateBack({
      delta: 1
    })

  },
  onShow(){
    let that = this;
    app.request({
      url: api.driver.getQrcode,
      method: 'post',
      success: function (res) {
        // console.log('zm')
        // console.log(res)
        that.setData({
          authorize: res.img
        })
      }
    })
  },
  getUserInfo: function (e) {
    let that = this;
	var scene = wx.getStorageSync('scene') ? wx.getStorageSync('scene'):'';
    this.getSettings()
    wx.showLoading({})
    wx.login({
      success: res1 => {
        // console.log(res1);
        if (res1.code) {
          wx.getUserInfo({
            success: res => {
              // console.log(res);
              wx.hideLoading()
              app.request({
                url: api.user.login,
                method: 'post',
                data: {
                  code: res1.code,
                  iv: res.iv,
                  encryptedData: res.encryptedData,
				  id: scene
                },
                success: function (rs) {
                  //获取到用户详细信息
                  // console.log(rs);
                  if (rs.code == 1) {
                    // is_binding 0 绑定手机号 1 未绑定手机号
                    var is_binding = rs.data.is_binding;
                    //is_binding=0; //测试

                    // wx.showToast({
                    //   title: '登录成功',
                    //   icon: 'success',
                    //   duration: 2000
                    // })
                    app.showToast('登录成功',1,1000,()=>{
                      wx.navigateBack({
                        delta: 1
                      })
                    })
                    
                    wx.setStorageSync("user_info", rs.data)
                    wx.setStorageSync("uid", rs.data.id)
                    that.setData({
                      authStatus: true,
                      is_GZH: true,
                    })
                    wx.setStorageSync("access_token", rs.data.access_token)
                    //that.order_check()
                    // that.login_succ_func(rs.data)
                  } else {
                    wx.removeStorage({
                      key: 'access_token',
                    })
                    wx.removeStorage({
                      key: 'uid',
                    })
                    wx.showToast({
                      title: '登录失败',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                },
              })
            }
          })
        }
      }
    })
  },
  getSettings: function () {
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res), '初始化进入该页面')

        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      wx.getLocation({
                        success: function (res) { },
                      })

                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          wx.getLocation({
            success: function (res) { },
          })
        }
        else {
          //调用wx.getLocation的API
          wx.getLocation({
            success: function (res) { },
          })
        }
      }
    })
  },
})
